//
//  PlistFileHelper.m
//  SaiNiuHui
//
//  Created by 吕飞俊 on 16/2/17.
//  Copyright © 2016年 国都快易科技（深圳）有限公司. All rights reserved.
//

#import "PlistFileHelper.h"
#import "StringUtils.h"

static NSString *kkey = @"Open an account";

@implementation PlistFileHelper

#pragma mark save img
+(BOOL)fileExistsAtPath:(NSString *)filePath{
    NSFileManager* fm = [NSFileManager defaultManager];
    return [fm fileExistsAtPath:filePath];
}

+(UIImage *)getimgData:(NSString *)key uid:(NSString *)uid{
    NSString *userFilePath=[PlistFileHelper getImgUserFilePath:uid];
    NSString *imagePath = [userFilePath stringByAppendingPathComponent:[key stringByAppendingString:@".png"]];
    return [UIImage imageWithContentsOfFile:imagePath];
}

+(NSString *)getimgPath:(NSString *)imgName uid:(NSString *)uid{
    NSString* filePath = [PlistFileHelper getImgUserFilePath:uid];
    return [filePath stringByAppendingPathComponent:imgName];
}

+(NSString *)getImgUserFilePath:(NSString *)uid{
    NSString *openFilePath = [PlistFileHelper getImgOpenFilePath];
    return [openFilePath stringByAppendingPathComponent:uid];
}

+(NSString *)getImgOpenFilePath{
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* filePath = [paths objectAtIndex:0];
    return [filePath stringByAppendingPathComponent:kkey];
}

//创建文件夹
+(void)createDirectoryAtPath:path{
    NSFileManager* fm = [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:path]) {
        [fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

//创建文件
+(void)createFileAtPath:filePath{
    NSFileManager* fm = [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:filePath]) {
        [fm createFileAtPath:filePath contents:nil attributes:nil];
    }
    NSLog(@"%@",filePath);
}

+(NSString *)getFilePathName:(NSString *)docName{
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* filePath = [paths objectAtIndex:0];
    return [filePath stringByAppendingPathComponent:docName];
}

#pragma mark save json
+(void)saveDicToJson:(id)dic key:(NSString *)key {
    return [PlistFileHelper saveDicToJson:dic key:key docName:@"temp.json"];
}
+(void)saveDicToJson:(id)dic key:(NSString *)key docName:(NSString *)docName{
#if SavePlistFile
    return;
#endif
    
    NSString *filePath=[PlistFileHelper getFilePathName:docName];
    [PlistFileHelper createFileAtPath:filePath];
    
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
    if (!dictionary) {
        dictionary = [NSMutableDictionary dictionary];
    }
    if (dic) {
        if (key) {
            [dictionary setObject:dic forKey:key];
        }else{
            dictionary = [dic copy];
        }
        
        // 首先初始化一下数据流， path 是本地沙盒中的一个路径
        NSOutputStream *outStream = [[NSOutputStream alloc] initToFileAtPath:filePath append:NO];
        // 打开数据流
        [outStream open];
        
        [NSJSONSerialization writeJSONObject:dic toStream:outStream options:NSJSONWritingPrettyPrinted error:nil];
        [outStream close];
    }
}

#pragma mark save plist

+(void)saveDicToPlist:(id)dic key:(NSString *)key{
    return [PlistFileHelper saveDicToPlist:dic key:key docName:@"CacheOffline.plist"];
}
+(void)saveDicToPlist:(id)dic key:(NSString *)key docName:(NSString *)docName{
#if SavePlistFile
    return;
#endif
    
    NSString *filePath=[PlistFileHelper getFilePathName:docName];
    [PlistFileHelper createFileAtPath:filePath];
    
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
    if (!dictionary) {
        dictionary = [NSMutableDictionary dictionary];
    }
    if (dic) {
        [dictionary setObject:dic forKey:key];
    }else{
        dictionary = [dic copy];
    }
    [dictionary writeToFile:filePath atomically:YES];
}

+(NSDictionary *)readToPlist:(NSString *)key fileName:(NSString *)fileName{
    
    NSString *filePath=[PlistFileHelper getFilePathName:fileName];
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:filePath];
    return key?dictionary[key]:dictionary;
}

#pragma mark html
+(void)saveCatchHtml:(NSString *)urlString{
#if SaveHtmlFile
    return;
#endif
    if (![StringUtils isEmptyString:urlString]) {
        NSURL *url = [NSURL URLWithString:urlString];
        NSData  *data = [NSData dataWithContentsOfURL:url];
        NSString* filePath = [self getCatchHtmlPath:urlString];
        [PlistFileHelper createFileAtPath:filePath];
        [data   writeToFile:filePath atomically:NO];
    }
}

+(NSString *)getCatchHtmlPath:(NSString *)urlString{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [paths[0] stringByAppendingPathComponent:[PlistFileHelper getCatchHtmlName:urlString]];
    return [NSString stringWithFormat:@"%@.html",filePath];
}

+(NSString *)getCatchHtmlName:(NSString *)urlString{
    //    NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"^\\w+$" options:0 error:NULL];
    NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"\\W" options:0 error:NULL];
    //    NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"^[a-zA-Z0-9]" options:0 error:NULL];
    NSString *name = [expression stringByReplacingMatchesInString:urlString options:0 range:NSMakeRange(0, urlString.length) withTemplate:@""];
    return name;
}

@end
